package com.example.xd.myapplication;

import android.content.Intent;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.example.xd.model_permission.NotificationsUtils;
import com.example.xd.model_permission.float_window_permission.FloatWindowManager;

@Route(path = "/myapplication/NotifictionDialogActivity")
public class NotifictionDialogActivity extends AppCompatActivity {

    private Bundle mExtras;
    private String mPath;
    private String mType;
    private TextView mTv_dialog_context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notifiction_dialog);
        mTv_dialog_context = findViewById(R.id.tv_dialog_context);
        mExtras = getIntent().getBundleExtra("extras");
        mPath = getIntent().getStringExtra("path");
        mType = getIntent().getStringExtra("type");
        switch (mType) {
            case "notic":
                mTv_dialog_context.setText("获取通知栏权限");
                break;
            case "float":
                mTv_dialog_context.setText("获取悬浮窗权限");
                break;
        }

        findViewById(R.id.btn_confirm).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (mType) {
                    case "notic":
                        Intent intent = new Intent();
                        intent.setAction(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
                        intent.putExtra("app_package", getPackageName());
                        intent.putExtra("app_uid", getApplicationInfo().uid);
                        startActivity(intent);
                        break;
                    case "float":
                        FloatWindowManager.getInstance().applyPermission(NotifictionDialogActivity.this);
                        break;
                }

            }
        });


        findViewById(R.id.btn_off).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(NotifictionDialogActivity.this, "没有权限，部分功能无法体验", Toast.LENGTH_SHORT).show();
                finish();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        switch (mType) {
            case "notic":
                if (NotificationsUtils.isNotificationEnabled(this)) {
                    ARouter.getInstance()
                            .build(mPath)
                            .with(mExtras)
                            .navigation();
                    finish();
                }
                break;
            case "float":
                if (FloatWindowManager.getInstance().checkPermission(this)) {
                    ARouter.getInstance()
                            .build(mPath)
                            .with(mExtras)
                            .navigation();
                    finish();
                }
                break;
        }
    }
}
